PowerTCP SSH and SFTP for .NET
Read(Byte[]) Method
Example 




The byte array used to store the received data.
Receives data from the remote host.
Syntax
'Declaration
 
Public Overloads Function Read( _
   ByVal buffer() As Byte _
) As Data
'Usage
 
Dim instance As SessionStream
Dim buffer() As Byte
Dim value As Data
 
value = instance.Read(buffer)
public Data Read( 
   byte[] buffer
)
public: Data* Read( 
   byte[]* buffer
) 
public:
Data^ Read( 
   array<byte>^ buffer
) 

Parameters

buffer
The byte array used to store the received data.

Return Value

A Data object encapsulating the received data.
Remarks
This method will read as much data as is available, up to the size of the buffer.
Example
The example below demonstrates how to receive data without blocking the UI.
private void button1_Click(object sender, System.EventArgs e)
{
    //Connect and log into the server. Can be executed on a worker 
    //thread to not block the UI; executed synchronously here for simplicity
    connectAndLogin(myServerHostname, myServerPort, myUsername, myPassword);
    //Execute the specified 'receiveData' method on a worker thread to not block the UI
    ssh1.Start(receiveData, null);
}

private void connectAndLogin(string hostname, int port, string username, string password)
{
    //Connect to the server
    ssh1.Connection.RemoteEndPoint.HostNameOrAddress = hostname;
    ssh1.Connection.RemoteEndPoint.Port = port; //Usually 22
    ssh1.Connect();

    //Authenticate
    SshLoginData loginDetails = new SshLoginData();
    loginDetails.Username = username;
    loginDetails.Password = password;
    ssh1.Authenticate(loginDetails);
}

/// <summary>
/// Receives data from the server and marshals it to the UI thread for display.
/// </summary>
/// <param name="notUsed">Not used in this snippet</param>
private void receiveData(object notUsed)
{
    //Establish a shell
    SessionStream session = ssh1.StartShell();

    //Receive data when it is sent by the remote host
    //Marshal it to the UI for display
    while (true)
    {
        byte[] buf = new byte[1024];
        Data data = session.Read(buf);
        //If data.Count is 0, the connection has been closed, so break out of the loop.
        if (data.Count == 0)
            break;
        ssh1.Marshal(data, session, "", null);
    }
}

/// <summary>
/// Raised on the UI thread, when ssh1.Marshal is called in receiveData.
/// </summary>
private void ssh1_Data(object sender, SshDataEventArgs e)
{
    textBox1.AppendText(e.Data.ToString());
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    'Connect and log into the server. Can be executed on a worker 
    'thread to not block the UI; executed synchronously here for simplicity
    connectAndLogin(myServerHostname, myServerPort, myUsername, myPassword)
    'Execute the specified 'receiveData' method on a worker thread to not block the UI
    ssh1.Start(AddressOf receiveData, Nothing)
End Sub

Private Sub connectAndLogin(ByVal hostname As String, ByVal port As Integer, ByVal username As String, ByVal password As String)
    'Connect to the server
    ssh1.Connection.RemoteEndPoint.HostNameOrAddress = hostname
    ssh1.Connection.RemoteEndPoint.Port = port 'Usually 22
    ssh1.Connect()

    'Authenticate
    Dim loginDetails As New SshLoginData()
    loginDetails.Username = username
    loginDetails.Password = password
    ssh1.Authenticate(loginDetails)
End Sub

''' <summary>
''' Receives data from the server and marshals it to the UI thread for display.
''' </summary>
''' <param name="notUsed">Not used in this snippet</param>
Private Sub receiveData(ByVal notUsed As Object)
    'Establish a shell
    Dim session As SessionStream = ssh1.StartShell()

    'Receive data when it is sent by the remote host
    'Marshal it to the UI for display
    Do
        Dim buf(1023) As Byte
        Dim data As Data = session.Read(buf)
        'If data.Count is 0, the connection has been closed, so break out of the loop.
        If data.Count = 0 Then
            Exit Do
        End If
        ssh1.Marshal(data, session, "", Nothing)
    Loop
End Sub

''' <summary>
''' Raised on the UI thread, when ssh1.Marshal is called in receiveData.
''' </summary>
Private Sub ssh1_Data(ByVal sender As Object, ByVal e As SshDataEventArgs) Handles ssh1.Data
    textBox1.AppendText(e.Data.ToString())
End Sub
See Also

Reference

SessionStream Class
SessionStream Members
Overload List


PowerTCP SSH and SFTP for .NET Documentation Version 7.0
© 2023 Dart Communications. All Rights Reserved.
Send comments on this topic